package com.example.shushiyunsst.fragment.main.transaction.child.kline_chart

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.viewpager.widget.ViewPager
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.LazyFragment
import com.example.library_common.utils.even.MessageWrap
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.transaction.child.CHANG_ICON
import com.example.shushiyunsst.fragment.main.transaction.child.TRANSFER_INDEX_DATA_REFRESH
import com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.k_line_chart_portrait_content.*
import com.example.shushiyunsst.fragment.main.transaction.child.transfer_index_data
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.AppBarLayout.Behavior.DragCallback
import com.google.android.material.tabs.TabLayout
import org.greenrobot.eventbus.EventBus


class KlineChartPortraitContentFragment : LazyFragment(), View.OnClickListener,
    SwipeRefreshLayout.OnRefreshListener, AppBarLayout.OnOffsetChangedListener {

    companion object {
        fun newInstance(): KlineChartPortraitContentFragment {
            val args = Bundle()
            val fragment = KlineChartPortraitContentFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var transactionUIInvokeListener: KlineChartInterface? = null

    fun setTransactionUIInvokeListener(listener: KlineChartInterface?) {
        this.transactionUIInvokeListener = listener

    }

    private lateinit var mTabLayout: TabLayout

    private lateinit var mViewPager: ViewPager

    private lateinit var mIconTv: TextView

    private lateinit var moneySelector: ImageButton

    private lateinit var bankIcon: ImageButton

    private lateinit var mAppBarLayout: AppBarLayout

    private lateinit var mRefreshLayout: SwipeRefreshLayout

    private var mPagerAdapter: FragmentStatePagerAdapter? = null

    private var pageList = listOf<BaseFragmentEx>(
        KlineChartPortraitContentDeepFragment.newInstance(),
        KlineChartPortraitContentTradeFragment.newInstance(),
        KlineChartPortraitContentShortFragment.newInstance()
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    @SuppressLint("SetTextI18n")
    override fun lazyInit() {
        val bean = transactionUIInvokeListener?.getTransIndexNode()?.iconType

        val icon = bean?.tokenName ?: ""
        val cny = bean?.getToTransferIcon() ?: ""
        mIconTv.text = "$icon / $cny"


        onRefresh()
    }


    override fun initView(view: View) {
        val newInstance = KLineChartPanelFragment.newInstance()
        newInstance.setTransactionUIInvokeListener(transactionUIInvokeListener)
        loadRootFragment(R.id.k_line_chart_panel, newInstance)

        mAppBarLayout = view.findViewById(R.id.mAppBarLayout)

        mRefreshLayout = view.findViewById(R.id.mRefreshLayout)

        mRefreshLayout.setOnRefreshListener(this)

        mAppBarLayout.addOnOffsetChangedListener(this)

        mTabLayout = view.findViewById(R.id.mTabLayout)
        mViewPager = view.findViewById(R.id.mViewPager)
        mIconTv = view.findViewById(R.id.mIconTv)
        moneySelector = view.findViewById(R.id.money_selector)
        bankIcon = view.findViewById(R.id.bankIcon)
        bankIcon.setOnClickListener(this)
        moneySelector.setOnClickListener(this)

        for (e in pageList) {
            val mLabel = e as KlineChartPortraitLabel
            mLabel.setTransactionUIInvokeListener(transactionUIInvokeListener)
        }

        mPagerAdapter = object
            :
            FragmentStatePagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

            override fun getCount() = pageList.size

            override fun getItem(position: Int) = pageList[position]

            override fun getPageTitle(position: Int): CharSequence? {
                val mLabel = getItem(position) as KlineChartPortraitLabel
                return mLabel.title
            }

        }

        mViewPager.adapter = mPagerAdapter

        mTabLayout.setupWithViewPager(mViewPager)

        if (!mRefreshLayout.isRefreshing) {
            mRefreshLayout.isRefreshing = true
        }
    }

    override fun getLayoutId(): Any {
        return R.layout.k_line_chart_portrait_content_fragment
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.money_selector -> {
                transactionUIInvokeListener?.toggleDrawerLayout()
            }
            R.id.bankIcon -> {
                transactionUIInvokeListener?.onBackPage()
            }
        }
    }

    override fun handMessage(msg: MessageWrap?) {
        super.handMessage(msg)
        when (msg?.message) {
            transfer_index_data -> {
                lazyInit()
            }
        }
    }

    override fun onRefresh() {

        if (mRefreshLayout.isRefreshing) {
            mRefreshLayout.postDelayed({
                mRefreshLayout.isRefreshing = false
                val instance = MessageWrap.getInstance(TRANSFER_INDEX_DATA_REFRESH)
                EventBus.getDefault().post(instance)
            }, 1000)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mAppBarLayout.removeOnOffsetChangedListener(this)
    }

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
        mRefreshLayout.isEnabled = verticalOffset >= 0
    }
}